package xu.zhi.qing.user.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xu.zhi.qing.common.entity.Pagination;
import xu.zhi.qing.common.service.BaseService;
import xu.zhi.qing.exception.LoginErrorException;
import xu.zhi.qing.exception.UserRepeatException;
import xu.zhi.qing.user.entity.User;

import java.util.ArrayList;
import java.util.List;

@Transactional
@Service
public class UserService extends BaseService<User> {

	@Transactional(rollbackFor=Exception.class)
	public void saveUser(User user) throws UserRepeatException{
		//1.判断用户名是否重复
		if (userDao.findByUsername(user.getUsername())!=null) {
			throw new UserRepeatException("用户名重复！");
		}
		//2.保存用户
		userDao.save(user);
	}

	public User login(User user) throws LoginErrorException{
		//1.判读用户名是否存在
		User userReturn = userDao.findByUsername(user.getUsername());
		if (userReturn==null) {
			throw new LoginErrorException("用户不存在!");
		}
		//2.判读密码是否正确
		if (!userReturn.getPassword().equals(user.getPassword())) {
			throw new LoginErrorException("密码不正确!");
		}
		return userReturn;
	}

	public User findByUsername(String username) {
		return userDao.findByUsername(username);
	}

	public JSONObject getUserList(int page, int rows, String username) {
		Pagination<String> pagination = new Pagination<>();
		List<String> params = new ArrayList<>();
		params.add(username);
		pagination.setPage(page);
		pagination.setRows(rows);
		pagination.setData(params);
		List<User> result = userDao.findByPagination(pagination);
		Pagination<User> users = new Pagination<>();
		users.setData(result);
		users.setRows(rows);
		users.setPage(page);
		users.setCount(userDao.countByLikeUsername(username));
		JSONArray jUserList=new JSONArray();
		for (User user : users.getData()) {
			JSONObject jUser=new JSONObject();
			jUser.put("id", user.getId());
			jUser.put("username", user.getUsername());
			jUser.put("imgPath", user.getImgPath());
			jUser.put("gender", user.getGender());
			jUser.put("status", user.getStatus());
			jUserList.add(jUser);
		}
		JSONObject data=new JSONObject();
		data.put("data", jUserList);
		data.put("page", page);
		data.put("rows", rows);
		data.put("count", users.getCount());
		return data;
	}

	public JSONObject getProfile(int id) {
		JSONObject profile=new JSONObject();
		User user = userDao.get(id);
		profile.put("id", user.getId());
		profile.put("username", user.getUsername());
		profile.put("imgPath", user.getImgPath());
		profile.put("status", user.getStatus());
		profile.put("shareCount", shareDao.countByUserId(user.getId()));
		profile.put("fansCount", followDao.countByUserId(user.getId()));
		profile.put("followCount", followDao.countByFansId(user.getId()));
		return profile;
	}

	public JSONArray getSearchUsers(String condition) {
		JSONArray userList=new JSONArray();
		if(condition==null||condition.length()==0){
			return userList;
		}
		//根据用户名搜索用户
		List<User> users = userDao.findAllByUsername("%"+condition+"%");

		for (User user : users) {
			JSONObject jUser=new JSONObject();
			jUser.put("id", user.getId());
			jUser.put("username", user.getUsername());
			jUser.put("imgPath", user.getImgPath());
			jUser.put("gender", user.getGender());
			userList.add(jUser);
		}
		return userList;
	}
}
